<?php
namespace WebApper\Booking;

class Booking extends \WebApper\Base {

	/**
	 * The booking id
	 *
	 * @var integer
	 */
	var $booking_id;

	/**
	 * The service id
	 *
	 * @var integer
	 */
	var $service_id;

	/**
	 * The week number 
	 *
	 * @var integer
	 */
	var $week_num;

	/**
	 * The reservation date 
	 *
	 * @var string
	 */
	var $reserveDate;

	/**
	 * The 	reservation time
	 *
	 * @var string
	 */
	var $reserveTime;

	/**
	 * The user id 	
	 *
	 * @var integer
	 */
	var $user_id;

	/**
	 * The 	post id
	 *
	 * @var integer
	 */
	var $postID;


	/** Public Methods ********************************************************/

	/**
	 * Constructor
	 *
	 * @param integer $booking_id
	 */
	function __construct( $booking_id = 0 ) {
		if ( !empty( $booking_id) ) {
			$this->booking_id = $booking_id;
			$this->populate();
		}					
	}

	/**
	 * Update or insert reservation booking details into the database.
	 *
	 * @global wpdb $wpdb WordPress database object
	 * @return bool Success or failure
	 */
	function save() {
		global $webapper, $wpdb;
		// Update
		if ( !empty( $this->booking_id ) ) {
			$result = $wpdb->update( 
				$webapper->tables->booking, 
				array( 
					'service_id' => $this->service_id,
					'week_num' => $this->week_num,
					'reserveDate' => $this->reserveDate,
					'reserveTime' => $this->reserveTime,
					'user_id' => $this->user_id,
					'postID' => $this->postID
					), 
				array( 'booking_id' => $this->booking_id ), 
				array(  '%d', '%d', '%s', '%s', '%d', '%d' ), 
				array( '%d' ) 
			);

		// Insert
		} else {
			$result = $wpdb->insert( 
				$webapper->tables->booking, 
				array( 
					'service_id' => $this->service_id,
					'week_num' => $this->week_num,
					'reserveDate' => $this->reserveDate,
					'reserveTime' => $this->reserveTime,
					'user_id' => $this->user_id,
					'postID' => $this->postID
					), 
				array(  '%d', '%d', '%s', '%s', '%d', '%d' ) 
			);
			$this->booking_id = $wpdb->insert_id;
		}
	
		if ( !$result )
			return false;

		return true;
	}
	/**
	 * Fetche the reservation booking user data from the database.
	 * @global $webapper
	 * @global $wpdb WordPress database object
	 */
	 public function get_booking_user_id()
	 {
		global $webapper, $wpdb;
		$serviceTable 	= $webapper->tables->services;
		$scheduleTable 	= $webapper->tables->schedules;
		$bookingTable = $webapper->tables->booking;
		
		return $wpdb->get_results($wpdb->prepare("select distinct user_id from {$bookingTable} where service_id=%s and reserveDate=%s and week_num=%d", $this->service_id, $this->reserveDate, $this->week_num));
		 
	 }
	 
	 /**
	 * Fetch the reservation booking creation date by user id.
	 * @global $webapper
	 * @global $wpdb WordPress database object
	 */
	 public function get_creation_date_by_user_id()
	 {
		global $webapper, $wpdb;
		$serviceTable 	= $webapper->tables->services;
		$scheduleTable 	= $webapper->tables->schedules;
		$bookingTable = $webapper->tables->booking;
		return $wpdb->get_results($wpdb->prepare("select creation_date from {$bookingTable} where service_id=%d and reserveDate=%s and week_num=%d and user_id=%d order by creation_date desc limit 0,1", $this->service_id, $this->reserveDate, $this->week_num, $this->user_id));		 
	 }
	 
	  /**
	 * get the first reservation user_id.
	 * @global $webapper
	 * @global $wpdb WordPress database object
	 */
	 public function get_user_id_by_fast_booking_date($minTime)
	 {
		global $webapper, $wpdb;
		$serviceTable	= $webapper->tables->services;
		$scheduleTable 	= $webapper->tables->schedules;
		$bookingTable	= $webapper->tables->booking;
		return $wpdb->get_results($wpdb->prepare("select user_id from {$bookingTable} where creation_date=%s",$minTime), ARRAY_A);		 
	 }

	/** Private Methods *******************************************************/

	/**
	 * Fetches the reservation booking data from the database.
	 *
	 * @global $webapper
	 * @global $wpdb WordPress database object
	 */
	private function populate() {
		global $webapper, $wpdb;
		if ( $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$webapper->tables->booking} WHERE booking_id = %d", $this->booking_id ) ) ) {
			$this->booking_id	= $booking->booking_id;
			$this->service_id	= $booking->service_id;
			$this->week_num		= $booking->week_num;
			$this->reserveDate	= $booking->reserveDate;
			$this->reserveTime	= $booking->reserveTime;
			$this->user_id		= $booking->user_id;
			$this->postID		= $booking->postID;	
		}
	}	
}

?>